﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using MySql.Data.MySqlClient;
namespace DAL
{
    public  class packetruleDal
    {
        /// <summary>
        /// 添加规则
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static int Addreule(string merchantid, string rule,string pk_ruletitle)
        {
            string sql = "insert into ms_packetrule(pk_merchantid,pk_rule,pk_date,pk_ruletitle) values(@pk_merchantid,@pk_rule,@pk_date,@pk_ruletitle)";
            MySqlParameter[] values = new MySqlParameter[4];
            values[0] = new MySqlParameter("pk_merchantid", merchantid);
            values[1] = new MySqlParameter("pk_rule", rule);
            values[2] = new MySqlParameter("pk_date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[3] = new MySqlParameter("pk_ruletitle", pk_ruletitle);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查商户规则
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static List<ms_packetrule> Querybyid(string merchantid)
        {
            string sql = "select * from ms_packetrule where pk_merchantid=@pk_merchantid";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("pk_merchantid", merchantid);
            List<ms_packetrule> rulelist = new List<ms_packetrule>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_packetrule rule = new ms_packetrule();
                rule.pk_id = rd["pk_id"].ToString();
                rule.pk_merchantid = rd["pk_merchantid"].ToString();
                rule.pk_rule = rd["pk_rule"].ToString();
                rule.pk_date = rd["pk_date"].ToString();
                rule.pk_ruletitle = rd["pk_ruletitle"].ToString();
                rule.pk_isdel = rd["pk_isdel"].ToString();
                rulelist.Add(rule);
            }
            rd.Close();
            return rulelist;
        }
        /// <summary>
        /// 修改规则
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static int Updaterule(string merchantid, string rule)
        {
            string sql = "update ms_packetrule set pk_rule=@pk_rule where pk_merchantid=@pk_merchantid";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("pk_merchantid", merchantid);
            values[1] = new MySqlParameter("pk_rule", rule);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 删除规则
        /// </summary>
        /// <param name="ruleid"></param>
        /// <returns></returns>
        public static int Delrule(string ruleid)
        {
            string sql = "update ms_packetrule set pk_isdel=2 where pk_id=@pk_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("pk_id", ruleid);
            return DBHelper.ExcutUpdate(sql, values);
        }

    }
}
